/**
 * 剑指 Offer 56 - II. 数组中数字出现的次数 II
 * https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/
 */

/**
 * @param {number[]} nums
 * @return {number}
 */
function singleNumber(nums) {
  let result = 0;
  let sign = 1;
  for (let i = 0; i < 32; i += 1, sign <<= 1) {
    let count = 0;
    for (let j = 0; j < nums.length; j += 1) {
      if (sign & nums[j]) {
        count += 1;
      }
    }
    if (count % 3) {
      result |= sign;
    }
  }
  return result;
}

console.log(singleNumber([3, 4, 3, 3]) === 4);
console.log(singleNumber([9, 1, 7, 9, 7, 9, 7]) === 1);
